package com.iye.puretennis.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.Switch;
import com.iye.puretennis.R;
import com.iye.puretennis.custom.HuaWenHuPoEditText;
import com.iye.puretennis.domain.GroupAccess;
import com.iye.puretennis.domain.UrlSuffix;
import com.iye.puretennis.domain.Group;
import com.iye.puretennis.helper.Constant;
import com.iye.puretennis.helper.RemoteUrlCollections;
import com.iye.puretennis.helper.ServerAccessHelper;
import com.iye.puretennis.helper.Session;
import com.iye.puretennis.utils.StringUtils;
import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 * User: jing wu
 * Date: 2014/11/25
 * Time: 9:14
 */
public class CreateGroupFragment extends TabFragment {

    public static final String TAG = "create_group";

    private Switch allSearchOn;

    private Switch friendsSearchOn;

    private Group group;



    @Override
    public void onCustomCreateView(LayoutInflater inflater) {

        Session session = Session.newSession();

        group = (Group) session.get("group");

        ImageButton saveButton = (ImageButton) mainView.findViewById(R.id.save_button);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createGroup();
            }
        });

        allSearchOn = (Switch) mainView.findViewById(R.id.all_search_on);

        allSearchOn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                friendsSearchOn.setChecked(!isChecked);
            }
        });

        friendsSearchOn = (Switch) mainView.findViewById(R.id.friends_search_on);

        friendsSearchOn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                allSearchOn.setChecked(!isChecked);
            }
        });

        if (group != null) {
            render(group);
        }
    }


    private void render(Group group) {
        HuaWenHuPoEditText groupNameText = (HuaWenHuPoEditText) mainView.findViewById(R.id.group_name);

        groupNameText.setText(group.getName());

        String access = group.getAccess();

        if (GroupAccess.PUBLIC.name().equals(access)) {
            allSearchOn.setChecked(true);
            friendsSearchOn.setChecked(false);
        } else {
            allSearchOn.setChecked(false);
            friendsSearchOn.setChecked(true);
        }

        HuaWenHuPoEditText descriptionText = (HuaWenHuPoEditText) mainView.findViewById(R.id.group_description);

        String description = group.getDescription();

        descriptionText.setText(description == null ? "" : description);
    }


    @Override
    public int getViewId() {
        return R.layout.creategroup;
    }

    @Override
    public int getSourceId() {
        return R.id.create_group;
    }

    @Override
    public void updateUI() {

    }

    public void createGroup() {

        HuaWenHuPoEditText groupNameText = (HuaWenHuPoEditText) mainView.findViewById(R.id.group_name);

        final String groupName = groupNameText.getText().toString();


        boolean accessPublic = allSearchOn.isChecked();

        String groupAccess = "FRIEND_ONLY";
        if (accessPublic) {
            groupAccess = "PUBLIC";
        }

        final String finalAccess = groupAccess;


        HuaWenHuPoEditText descriptionText = (HuaWenHuPoEditText) mainView.findViewById(R.id.group_description);

        final String description = descriptionText.getText().toString();

        if (StringUtils.hasText(groupName) && StringUtils.hasText(groupAccess)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    JSONObject groupJsonObj = new JSONObject();
                    try {
                        groupJsonObj.put("groupGuid", group == null ? "" : group.getGuid());
                        groupJsonObj.put("name", groupName);
                        groupJsonObj.put("access", finalAccess);
                        groupJsonObj.put("description", description);
                        StringEntity paramEntity = new StringEntity(groupJsonObj.toString(), Constant.CHARSET_UTF8);
                        String result = ServerAccessHelper.queryStringForPost(RemoteUrlCollections.getUrl(UrlSuffix.GROUP), token.getToken(), paramEntity);
                        if (!result.equals(Constant.RESPONSE_BADLY)) {
                            goFragment(R.id.create_group, "MyGroupsFragment", MyGroupsFragment.class, null, false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

}
